package com.ta.gulimall.common.interceptor;

import com.ta.gulimall.common.constant.AuthConstant;
import com.ta.gulimall.common.view.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class UserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberVo> userLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();

        MemberVo vo = (MemberVo) session.getAttribute(AuthConstant.LOGIN_USER);
        if (vo != null) {
            userLocal.set(vo);
            return true;
        }else{
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }

    }
}
